<?php
namespace App\Http\Controllers\ApiWx\Mys;

use App\DBs\GuoDB;
use Illuminate\Http\Request;

class GuoController extends BaseController
{
    //物品

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new GuoDB();
    }

    /**
     * 果树列表
     */
    public function getIndex(Request $request)
    {
        $wx = $request->post('wx','');
        $resDb = $this->db->getMyTreeIndex($wx);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data']; $total = $resDb['total'];
        return $this->toWxJson(0,$items,$total);
    }

    /**
     * 果树无组合列表
     */
    public function getGroupNoIndex(Request $request)
    {
        $wx = $request->post('wx','');
        $group = $request->post('group',0);
        $resDb = $this->db->getMyGroupNoIndex();
        $resTree = $this->db->getTreesByGroup($group);
        $items['treeNos'] = $resDb['code']==0 ? $resDb['data'] : array();
        $items['treeOks'] = $resTree;
        return $this->toWxJson(0,$items);
    }

    /**
     * 养护组合列表
     */
    public function getGroupIndex(Request $request)
    {
        $wx = $request->post('wx','');
        $resDb = $this->db->getMyGroupIndex($wx);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data']; $total = $resDb['total'];
        return $this->toWxJson(0,$items,$total);
    }

    /**
     * 养护组合新增
     */
    public function setGroupAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $name = $request->post('name','');
        $resDb = $this->db->setGroupAdd($wx,$name);
        if ($resDb['code']==-3) {
            return array('code'=>'-5','msg'=>$resDb['msg']);
        } else if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 组合新增果树
     */
    public function setTreeAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $gid = $request->post('gid',0);
        $tid = $request->post('tid',0);
        $resDb = $this->db->setGroupAddTree($wx,$gid,$tid);
        if ($resDb['code']==-3) {
            return array('code'=>'-5','msg'=>$resDb['msg']);
        } else if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 组合删除果树
     */
    public function setTreeDel(Request $request)
    {
        $wx = $request->post('wx','');
        $id = $request->post('id',0);
        $resDb = $this->db->setGroupDelTree($id);
        if ($resDb['code']==-3) {
            return array('code'=>'-5','msg'=>$resDb['msg']);
        } else if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 养护任务列表
     */
    public function getTaskIndex(Request $request)
    {
        $wx = $request->post('wx','');
        $resDb = $this->db->getMyTaskIndex($wx);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data']; $total = $resDb['total'];
        return $this->toWxJson(0,$items,$total);
    }

    /**
     * 养护任务新增
     */
    public function setTaskAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $title = $request->post('title','');
        $gid = $request->post('gid',0);
        $link = $request->post('link','');
        if (!$wx || !$title || !$gid || !$link) {
            return $this->toWxJson(-2);
        }
        $links = explode('/uploads/',$link);
        $link = 'uploads/'.$links[1];
        $resDb = $this->db->setTaskAdd($wx,$title,$gid,$link);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 养护任务删除
     */
    public function setTaskDel(Request $request)
    {
        $wx = $request->post('wx','');
        $id = $request->post('id',0);
        $resDb = $this->db->setTaskDel($id);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 收成列表
     */
    public function getCropIndex(Request $request)
    {
        $wx = $request->post('wx','');
        $resDb = $this->db->getMyCropIndex($wx);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data']; $total = $resDb['total'];
        return $this->toWxJson(0,$items,$total);
    }

    /**
     * 收成新增
     */
    public function setCropAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $title = $request->post('title','');
        $tid = $request->post('tid',0);
        $link = $request->post('link','');
        if (!$wx || !$title || !$tid || !$link) {
            return $this->toWxJson(-2);
        }
        $links = explode('/uploads/',$link);
        $link = 'uploads/'.$links[1];
        $resDb = $this->db->setCropAdd($wx,$title,$tid,$link);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 收成更新
     */
    public function setCropModify(Request $request)
    {
        $wx = $request->post('wx','');
        $id = $request->post('id',0);
        $guke = $request->post('guke','');
        $weight = $request->post('weight',0);
        $price = $request->post('price',0);
        if (!$wx) {
            return $this->toWxJson(-2);
        }
        $resDb = $this->db->setCropModify($id,$guke,$weight,$price);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 收成删除
     */
    public function setCropDel(Request $request)
    {
        $wx = $request->post('wx','');
        $id = $request->post('id',0);
        $resDb = $this->db->setCropDel($id);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }
}